package qhLoading.info 
{
	import core.ClassProxy;
	import flash.display.LoaderInfo;
	import flash.utils.ByteArray;
	import qhLoading.manager.CacheManager;
	import qhLoading.QhLoadItem;
	
	/**
	 * ...
	 * @author gcxk721
	 */
	public class ResourceInfo extends ClassProxy 
	{
	
		public var name : String;
        public var path : String;
        public var type : String;
		public var version : String;
		public var extData : Object;
        public var isLoaded : Boolean;
        public var isLoading : Boolean = false;
		public var loaclPath : String;
		public var priority : int = 0;
		
		protected var loaderInfo : LoaderInfo;
		
		private var _data : Object;
        private var _referenceCount : int = 0;
        private var _isDispose : Boolean = false;
        private var _soPath : String = null;
		
		public function ResourceInfo(item : QhLoadItem = null) 
		{
			 if (item != null)
            {
				name = item.name;
				path = item.path;
				type = item.type;
				version = item.version;
				loaclPath = path;
            }
		}
		
		public function get soPath() : String
        {
            var index : int = 0;
            if (_soPath == null)
            {
                if (path)
                {
                    index = path.lastIndexOf("?");
                    if (index > -1)
                    {
                        _soPath = path.slice(0, index);
                    }
                    else
                    {
                        _soPath = path;
                    }
                }
                else
                {
					_soPath = "";
                }
            }
            return _soPath;
        }
		
		public function get isDispose() : Boolean
        {
            return _isDispose;
        }
		
		public function set data(value : Object) : void
        {
            _data = value;
            _isDispose = false;
        }

        public function addReference() : void
        {
			_referenceCount += 1;
        }

        public function removeReference() : void
        {
           _referenceCount -= 1;
        }

        public function dispose() : void
        {
            _isDispose = true;
            _data = null;
            _referenceCount = 0;
            isLoaded = false;
            isLoading = false;
			extData = null;
            if (loaderInfo && loaderInfo.loader)
            {
                unload(loaderInfo);
            }
        }

        protected function unload(loaderInfo : LoaderInfo) : void
        {
            try
            {
                loaderInfo.loader.close();
            }
            catch (e:Error)
            {
            }
            loaderInfo.loader.unload();
        }
		
        public function get cacheBytes() : ByteArray
        {
            if (this.loaderInfo)
            {
                return this.loaderInfo.bytes;
            }
            return CacheManager.instance.readObject(name, version, soPath) as ByteArray;
        }
        
        public function set cacheBytes(bytes :ByteArray) : void
        {
            return CacheManager.instance.writeObject(name, version, bytes, soPath);
        }
		
	}

}